package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Olog;
import com.cloudwise.gproject.service.OlogService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 操作日志记录(Olog)表控制层
 *
 * @author makejava
 * @since 2021-03-22 19:06:46
 */
@Controller
@RequestMapping("olog")
public class OlogController {
    /**
     * 服务对象
     */
    @Resource
    private OlogService ologService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.ologService.queryById(id));
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toLog2")
    public String toLog2() {
        return "back/log2";
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Olog olog) {
        Date operTime = olog.getOperTime();
        if (operTime!=null){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(operTime);
            olog.setStartTime(format);
        }
        System.out.println(olog);
        PageInfo pageInfo = ologService.queryAll(page, limit, olog);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }
    /** 
     * @create by: 卫子健
     * @description: 动态获取所有的年份 
     * @create time: 2021/3/23 9:05
     * @param  
     * @return  
     */
    @ResponseBody
    @RequestMapping("queryYear")
    public Result queryYears() {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                0,ologService.queryYears() );
    }

    /**
     * 新增数据
     *
     * @param olog 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Olog olog) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.ologService.insert(olog));
    }

    /**
     * 修改数据
     *
     * @param olog 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(Olog olog) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.ologService.update(olog));
    }

    /**
     * 通过主键删除数据
     *
     * @param ologId 主键
     * @return 是否成功
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer ologId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.ologService.deleteById(ologId));
    }
    @ResponseBody
    @RequestMapping("queryShowChartData")
    public Result queryShowChartData(Integer year,Integer month){
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.ologService.queryShowChartData(year,month));
    }
}
